<style lang="less" scoped>
.modalTree {
  height: 500px !important;
  overflow: auto;
  padding-right: 10px;
}
</style>
<template>
  <div>

    <vxe-toolbar ref="toolbarRef" custom>

      <template #buttons>
        <Form inline @submit.native.prevent class="form-search">

          <FormItem>
            <Button type="primary" @click="AddData()" icon="md-add">新增</Button>
          </FormItem>
        </Form>

      </template>
    </vxe-toolbar>
    <vxe-table ref="xTable" id="role_index" border :height="tableHeight" :loading="loadingTable" size="small"
      align="center" :row-config="{ isHover: true }" :data="tableData" show-header-overflow show-overflow="tooltip">

      <vxe-column type="seq" title="序号" width="60">

      </vxe-column>
      <vxe-column title="操作" fixed="right" width="150">
        <template #default="{ row }">
          <Tooltip content="修改" placement="bottom-end">
            <Button type="primary" icon="ios-create" @click="tempData = row; modalData = true"
              style="margin-right:2px"></Button>
          </Tooltip>
          <Tooltip content="删除" placement="bottom-end">
            <Button type="error" icon="ios-trash" @click="DeleteRole(row.Id)" style="margin-right:2px"></Button>
          </Tooltip>
          <Tooltip content="授权" placement="bottom-end">
            <Button icon="md-build" type="success" style="margin-right:2px"
              @click="tempId = row.Id; modalMenu = true"></Button>
          </Tooltip>

        </template>
      </vxe-column>

      <vxe-column field="Name" title="角色名称" width="100"> </vxe-column>
      <vxe-column field="Remark" title="说明" min-width="100"> </vxe-column>

      <vxe-column field="Sort" title="排序" width="80"> </vxe-column>

    </vxe-table>

    <Modal :width="500" v-model="modalData" sticky draggable :title="tempData.Id > 0 ? '修改' : '新增'"
      :mask-closable="false">
      <Form ref="formValidate" :model="tempData" :label-width="100" :rules="ruleValidate">
        <FormItem label="角色名称" prop="Name">
          <Input v-model="tempData.Name" />
        </FormItem>


        <FormItem label="说明" prop="Remark">
          <Input v-model="tempData.Remark" />
        </FormItem>

        <FormItem label="排序" prop="Sort">
          <Input type="number" v-model="tempData.Sort" />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SaveRole">确定</Button>
      </div>
    </Modal>
    <Modal :width="900" draggable sticky v-model="modalMenu" title="授权" footer-hide>
      <Rolemenu v-if="modalMenu" :id="tempId" @on-success="modalMenu = false"></Rolemenu>
    </Modal>

  </div>
</template>

<script>
import {
  GetRoles,
  SaveRole,

  DeleteRole
} from '@/api/rbac'
import Rolemenu from './rolemenu.vue'
import { usertypeList } from '@/libs/enum'
export default {
  name: 'role',
  data() {
    return {
      usertypeList,
      total: 0,
      page: 1,
      size: 50,
      tableHeight: 450,
      keyword: '',
      ruleValidate: {
        Name: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      loadingTable: false,
      tempData: {
        UserType: 1,
        Name: '',
        Remark: ''
      },
      modalData: false,
      modalMenu: false,
      tableData: [],
      treeMenu: [],
      tempMenuIds: [],
      tempId: 0,
      checkAll: false
    }
  },
  computed: {},
  methods: {
    SaveRole() {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true
          SaveRole(vm.tempData).then((res) => {
            vm.loading = false
            if (res.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.GetRoles()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },
    GetRoles() {
      let vm = this
      vm.loadingTable = true
      GetRoles().then((res) => {
        vm.loadingTable = false
        if (res.StatusCode == 200) {
          vm.tableData = res.Data
        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },
    DeleteRole(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeleteRole(id).then((res) => {
            if (res.StatusCode == 200) {
              this.GetRoles()
              this.$Message.success({ title: '提示', content: '删除成功' })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => { }
      })
    },
    AddData() {
      var vm = this
      vm.modalData = true
      vm.tempData = {
        UserType: 1,
        Name: '',
        Remark: ''
      }
    }

  },
  mounted() {
    this.tableHeight = window.innerHeight - 240
    const $table = this.$refs.xTable
    const $toolbar = this.$refs.toolbarRef
    if ($table && $toolbar) {
      $table.connect($toolbar)
    }
    this.GetRoles()
  },
  components: { Rolemenu }
}
</script>

<style></style>
